package com.xoa.service.crm.bidding;

import com.xoa.dao.crm.bidding.CrmBiddingInfoMapper;
import com.xoa.dao.crm.bidding.CrmTenderInfoMapper;
import com.xoa.model.crm.bidding.CrmBiddingInfoWithBLOBs;
import com.xoa.model.crm.bidding.CrmTenderInfoExample;
import com.xoa.model.crm.bidding.CrmTenderInfoWithBLOBs;
import com.xoa.model.enclosure.Attachment;
import com.xoa.model.users.Users;
import com.xoa.service.department.DepartmentService;
import com.xoa.service.users.UsersService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.GetAttachmentListUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BiddingInfoService {

    @Resource
    private CrmBiddingInfoMapper biddingInfoMapper;

    @Resource
    private CrmTenderInfoMapper tenderInfoMapper;

    @Resource
    private DepartmentService departmentService;

    @Resource
    private UsersService usersService;

    /**
     * 作者 廉立深
     * 日期 2020/10/12
     * 方法介绍  招标发布列表接口
     * 参数 [request, biddingInfoWithBLOBs]
     * 返回 com.xoa.util.ToJson
     **/
    public ToJson getBiddingInfo(HttpServletRequest request, CrmBiddingInfoWithBLOBs biddingInfoWithBLOBs,Integer page ,Integer limit) {
        ToJson toJson = new ToJson(1,"false");
        Map map = new HashMap();
        try {
            PageParams pageParams = new PageParams();
            pageParams.setUseFlag(true);
            pageParams.setPageSize(limit);
            pageParams.setPage(page);
            map.put("pageParams",pageParams);
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
            String sqlType = "xoa" + request.getSession().getAttribute("loginDateSouse");

            if (!StringUtils.checkNull(biddingInfoWithBLOBs.getTenderUserName())){
                map.put("tenderUserName","0");
            }

            map.put("userId",users.getUserId());
            map.put("biddingId",biddingInfoWithBLOBs.getBiddingId());
            map.put("status",biddingInfoWithBLOBs.getStatus());
            map.put("projectYear",biddingInfoWithBLOBs.getProjectYear());
            List<CrmBiddingInfoWithBLOBs> biddingInfo = biddingInfoMapper.getBiddingInfo(map);
            biddingInfo.forEach ( item -> {

                //采购部门
                if (item.getPurchaseDeptid() != null){
                    item.setPurchaseDeptName(departmentService.getDpNameById(item.getPurchaseDeptid()));
                }

                //招标文件
                if (!StringUtils.checkNull(item.getAttachmentName()) && !StringUtils.checkNull(item.getAttachmentId())) {
                    List<Attachment> attachmentList = GetAttachmentListUtil.returnAttachment(item.getAttachmentName(), item.getAttachmentId(), sqlType, "certification");
                    item.setAttachmentList(attachmentList);
                }

                //投标人
                if (!StringUtils.checkNull(item.getTenderUserid())){
                    item.setTenderUserName(usersService.getUserNameById(item.getTenderUserid()));
                }

                //查看人
                if (!StringUtils.checkNull(item.getViewUserid())){
                    item.setViewUserName(usersService.getUserNameById(item.getViewUserid()));
                }

                //投标信息
                Map map1 = new HashMap();
                map1.put("userId",users.getUserId());
                map1.put("biddingId",item.getBiddingId());
                List<CrmTenderInfoWithBLOBs> tenderInfoList = tenderInfoMapper.getTenderInfo(map1);
                if (!tenderInfoList.isEmpty()){
                    item.setCrmTenderInfo(tenderInfoList.get(0));
                }

            });

            toJson.setObj(biddingInfo);
            PageParams pageParams = (PageParams) map.get("pageParams");
            toJson.setTotleNum(pageParams.getTotal());
            toJson.setFlag(0);
            toJson.setMsg("true");
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    /**
     * 作者 廉立深
     * 日期 2020/10/12
     * 方法介绍  编辑接口
     * 参数 [request, biddingInfoWithBLOBs]
     * 返回 com.xoa.util.ToJson
     **/
    public ToJson editBiddingInfo(HttpServletRequest request, CrmBiddingInfoWithBLOBs biddingInfoWithBLOBs) {
        ToJson toJson = new ToJson(1,"false");
        try {
            Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);

            //判断是否传入userID
            if (!StringUtils.checkNull(biddingInfoWithBLOBs.getReleaseUserid())){
                biddingInfoWithBLOBs.setReleaseUserid(users.getUserId());
            }

            if (biddingInfoWithBLOBs.getBiddingId() != null){
                biddingInfoMapper.updateByPrimaryKeySelective(biddingInfoWithBLOBs);
            }else{
                biddingInfoMapper.insertSelective(biddingInfoWithBLOBs);
            }
            toJson.setFlag(0);
            toJson.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    /**
     * 作者 廉立深
     * 日期 2020/10/12
     * 方法介绍  删除接口
     * 参数 [request, biddingId]
     * 返回 com.xoa.util.ToJson
     **/
    public ToJson delBiddingInfo(HttpServletRequest request, Integer biddingId) {
        ToJson toJson = new ToJson(1,"false");
        try {

            //删除招标信息
            biddingInfoMapper.deleteByPrimaryKey(biddingId);

            //删除投标信息
            CrmTenderInfoExample crmTenderInfoExample = new CrmTenderInfoExample();
            CrmTenderInfoExample.Criteria criteria = crmTenderInfoExample.createCriteria();
            criteria.andBiddingIdEqualTo(String.valueOf(biddingId));
            tenderInfoMapper.deleteByExample(crmTenderInfoExample);

            toJson.setFlag(0);
            toJson.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

     /**
     * 作者 陈东虎
     * 日期 2020/10/17
     * 方法介绍  发布招标接口
     * 参数 [request, biddingId]
     * 返回 com.xoa.util.ToJson
     **/
    public ToJson releaseBidding(HttpServletRequest request, String biddingIds){
        ToJson toJson = new ToJson(1,"false");
        try {
            if(!StringUtils.checkNull(biddingIds)){
                String[] biddingIdArr = biddingIds.split(",");
                Integer[] biddIngdArrInt = new Integer[biddingIdArr.length];
                //字符串数组转为int数组
                for(int i=0;i<biddingIdArr.length;i++){
                    String biddingId = biddingIdArr[i];
                    if(!StringUtils.checkNull(biddingId)){
                        biddIngdArrInt[i] = Integer.valueOf(biddingId);
                    }
                }
                int i = 0;
                if(biddIngdArrInt.length>0){
                    i = biddingInfoMapper.releaseBidding(biddIngdArrInt,"2");
                    if(i>0){
                        toJson.setFlag(0);
                        toJson.setMsg("发布成功");
                    }else{
                        toJson.setMsg("发布失败，请重试");
                    }
                }else{
                    toJson.setMsg("参数错误");
                }
            }else{
                toJson.setMsg("参数错误");
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }
}
